package com.jketing.entity.ams.sys;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.jketing.entity.BaseEntity;

/**
 * Role 系统角色实体
 * 
 * @author  李荣华
 * @version v1.0
 * @date     2017-08-16
 * @since   Jpa 2.1
 */
@JsonIgnoreProperties(value= {"accounts","popedoms"})
@Entity
@Table(name = "Role_")
public class Role extends BaseEntity {
	private static final long serialVersionUID = -3361844390607641895L;
	/**
	 * code:角色代码
	 */
	private String code;
	/**
	 * name:角色名称
	 */
	private String name;
	/**
	 * explain:角色说明 
	 */
	private String explain;
	/**
	 * accounts:账号集合
	 */
	private Set<Account> accounts = new HashSet<Account>();
	/**
	 * popedoms:权限集合
	 */
	private Set<Popedom> popedoms = new HashSet<Popedom>();

	/**
	 * code 角色代码
	 */
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}

	/**
	 * name 角色名称
	 */
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	/**
	 * explain  角色说明 
	 */
	@Column(name="explain_")
	public String getExplain() {
		return explain;
	}
	public void setExplain(String explain) {
		this.explain = explain;
	}
	
	/**
	 * accounts 此角色的账号列表
	 */
	@ManyToMany(mappedBy = "roles", cascade = {CascadeType.REFRESH },fetch=FetchType.LAZY)
	public Set<Account> getAccounts() {
		return accounts;
	}
	public void setAccounts(Set<Account> accounts) {
		this.accounts = accounts;
	}
	
	/**
	 * 增加账号
	 * @param Account
	 */
	public void add(Account obj){
		if (!accounts.contains(obj)) {
			accounts.add(obj);
		}
	}
	
	/**
	 * 删除账号
	 * @param Account
	 */
	public void remove(Account obj){
		if (accounts.contains(obj)) {
			accounts.remove(obj);
		}
	}

	/**
	 * 角色拥有的权限列表
	 */
	@OneToMany(mappedBy = "role", cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)	
	public Set<Popedom> getPopedoms() {
		return popedoms;
	}
	public void setPopedoms(Set<Popedom> popedoms) {
		this.popedoms = popedoms;
	}
	
	/**
	 * 增加权限
	 * @param popedom
	 */
	public void add(Popedom popedom) {
		if (!popedoms.contains(popedom)) {
			popedoms.add(popedom);
			popedom.setRole(this);
		}
	}
	
	/**
	 * 删除权限
	 * @param popedom
	 */
	public void remove(Popedom popedom) {
		if (popedoms.contains(popedom)) {
			Iterator<Popedom> iterator = popedoms.iterator(); 
		    while (iterator.hasNext()) {
		    	Popedom pd = iterator.next();
		        if (popedom.equals(pd)) {
			        iterator.remove();       //这行代码是关键。
			        //popedomList.remove(popedom);
			        popedom.setRole(null);
			        break;
		        }
		    }
		}
	}

	/**
	 * 	equals 判断实例是否相等
	 * 
	 * 	是本类实例并且实例id值相等返回true
	 * 	其它情况返回false
	 * 
	 * 	@param object 其它实例
	 */
	@Override
	public boolean equals(Object object) {
		if (!(object instanceof Role)) {
			return false;
		}
		Role other = (Role) object;
		if(!id.equals(other.id)){
			return false;
		}
		return true;
	}
}
